package com.wy.thread_safe.exclusive;

/**
 * @author 美女
 * @date 2022/04/23 23:00
 **/
public class Main {
    //这个对象用来当锁对象
    static Object lock=new Object();
    static class MyThread1 extends Thread{
        @Override
        public void run() {
            synchronized (lock) {
                for (int i = 0; i < 100000; i++) {
                    System.out.println("我是张三");
                }
            }
        }
    }
    static class MyThread2 extends Thread{
        @Override
        public void run() {
            synchronized (lock) {
                for (int i = 0; i < 100000; i++) {
                    System.out.println("我是李四");
                }
            }
        }
    }

    public static void main(String[] args) {
        Thread t1=new MyThread1();
        t1.start();
        Thread t2=new MyThread2();
        t2.start();
        //不加锁时，一会张三一会李四交替出现
        //加锁后（两个都是加锁的，看哪个锁先抢上），谁先抢到谁打印完才开始另一个的打印——印证互斥，即不能同时在执行，一定互斥在进行
    }
}
